package com.czz.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.QuestionComment;
import com.czz.portal.service.QuestionCommentService;
import com.czz.portal.vo.CommentNestedVo;
import com.czz.portal.vo.UserQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: QuestionCommentController
 * @Author Czz
 * @Date 2020/2/14
 * @Time 17:14
 **/
@RestController
@RequestMapping(value = "/question/comment")
public class QuestionCommentController {

    @Autowired
    QuestionCommentService commentService;

    /**
    * @Description 分页查询问答评论分页列表
    * @Param [page, limit, id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/14
    * @Time 20:35
    **/
    @GetMapping(value = "/{page}/{limit}/{id}")
    public R getCommentNestedVoPageList(@PathVariable Long page, @PathVariable Long limit, @PathVariable String id){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<CommentNestedVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(commentService.getCommentNestedVoPageList(pageParam, id));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 新增问答回复/二级回复
    * @Param [comment]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/15
    * @Time 10:04
    **/
    @PostMapping(value = "")
    public R saveComment(QuestionComment comment){
        if (ObjectUtils.isEmpty(comment)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        commentService.saveComment(comment);
        return R.ok();
    }

    /**
    * @Description 获取用户回答分页列表
    * @Param [page, limit, userId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/15
    * @Time 17:56
    **/
    @GetMapping(value = "/user/{page}/{limit}/{userId}")
    public R getUserCommentPageList(@PathVariable Long page,@PathVariable Long limit,@PathVariable String userId) {
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(userId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<UserQuestionVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(commentService.getUserCommentPageList(pageParam,userId));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 采纳评论为最佳答案
    * @Param [commentId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/16
    * @Time 20:40
    **/
    @PutMapping(value = "/best")
    public R updateCommentBest(QuestionComment questionComment){
        if (StringUtils.isEmpty(questionComment.getCommentId()) || StringUtils.isEmpty(questionComment.getUserId())) {
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        commentService.updateCommentBest(questionComment);
        return R.ok();
    }
}
